package view;

import java.io.File;
import java.util.HashMap;
import java.util.Set;

import javax.swing.JFrame;

import appli.Loggable;

import view.interfaces.ViewInterface;

import logs.LogLevelEnum;
import logs.LogTypesEnum;
import modele.SimulDynamicDataInterface;
import modele.interfaces.SimulationInterface;
import modele.parameters.SimulParameters;

import controler.AppliSIM;
import controler.interfaces.StepReportInterface;
import controler.interfaces.ViewControleurInterface;

@SuppressWarnings("serial")
public class View extends JFrame implements ViewInterface, Loggable {

	private AppliSIM application;
	private ViewControleurInterface controler;

	private boolean initOK;

	private int w, h;

	private ScreenManager scr;

	private CardPanel cardPanel;
	private EcranSimul_1 ecranSimul;
	private Set<String> availableSimulationsNames;

	@Override
	public void init(ViewControleurInterface controler) {
		initOK = false;
		this.controler = controler;
		setName();
		scr.updateRefToEcrans(this);
		w = scr.getCurrentDisplayMode().getWidth();
		h = scr.getCurrentDisplayMode().getHeight();
		this.setBounds(0, 0, w, h);
		this.setUndecorated(true);
		this.setResizable(false);

		EcranMenuPrincipal menu = new EcranMenuPrincipal(w, h, this);
		this.cardPanel = new CardPanel();
		cardPanel.setBounds(0, 0, w, h);
		cardPanel.add(menu, "menu");
		this.getContentPane().add(cardPanel);
		initOK = true;
		logInit();

		switchToMenu();

		setVisible(true);
	}

	private void switchToMenu() {
		cardPanel.showCard("menu");
	}

	public View(ScreenManager scr) {
		this.scr = scr;
	}

	@Override
	public void injectModel(SimulationInterface injectModel) {
		// TODO Auto-generated method stub

	}

	private void logInit() {
		String log = "";
		if (initOK) {
			log = " (" + this.getName() + ") " + "InitStatus OK";
			controler.log(LogLevelEnum.INFO, LogTypesEnum.Init, log);
		} else {
			log = " (" + this.getName() + ") " + "InitStatus KO";
			controler.log(LogLevelEnum.FATAL, LogTypesEnum.Init, log);
		}
	}

	@Override
	public void log(LogLevelEnum logLevel, LogTypesEnum LogType, String log) {
		controler.log(logLevel, LogType, log);
	}

	@Override
	public void setName() {
		setName("Main_Frame");
	}

	@Override
	public void askRunSim(String sim) {
		controler.askRunSim(sim);

	}

	@Override
	public void updateAfterStep(StepReportInterface stepReport) {
		ecranSimul.updateAfterStep(stepReport);

	}

	@Override
	public void initwithStaticData(SimulParameters parameters) {
		ecranSimul = new EcranSimul_1(w, h, this, parameters);
		cardPanel.add(ecranSimul, "simul");

	}

	@Override
	public void switchToSimulScreen() {
		cardPanel.showCard("simul");
	}

	@Override
	public void initWithDynamicData(SimulDynamicDataInterface dynData) {
		ecranSimul.SetDynData(dynData);

	}

	@Override
	public void quit() {
		controler.quit();

	}

	@Override
	public void triggerAStep() {
		controler.triggerAStep();

	}

	@Override
	public void setSimulationsNames(HashMap<String, File> availableSimulations) {
		this.availableSimulationsNames = availableSimulations.keySet();

	}

	public Set<String> getAvailableSimulationsNames() {
		return availableSimulationsNames;
	}

}